<template>
  <div>
    <el-dialog
      title="价格调整"
      :visible.sync="dialogVisible"
      width="750px"
      :close-on-click-modal="false"
      @close="close"
    >
      <el-form
        ref="form"
        v-loading="loading"
        :model="form"
        :rules="rules"
        label-suffix="："
        label-width="100px"
        size="small"
      >
        <el-form-item label="当前房型">
          <span>{{ productInfo.name }}</span>
        </el-form-item>
        <el-form-item label="基础价格">
          <span>{{ productInfo.basePrice/100 }}</span>
        </el-form-item>
        <el-form-item label="所选日期">
          <span v-for="(item,index) in form.days" :key="index">
            <span>{{ $momentMini(item).format("YYYY-MM-DD") }}</span>&nbsp;&nbsp;&nbsp;
          </span>
          <el-tag>所选日期若已配置了规则价格，保存当日价格将会覆盖原有的规则价格，实行所保存的当日价格</el-tag>
        </el-form-item>
        <el-form-item label="当日价格" prop="price">
          <el-input v-model.trim="form.price" style="width:300px" placeholder="请输入价格">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round :disable="loading" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { validateCustomTwoDecimal } from '@/utils/validate'
import { modifyBatchProductParams } from '../../../api/product'
export default {
  data() {
    /**
       * 校验方法相关
       */
    const validatorPrice = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入当日价格'))
      } else if (!validateCustomTwoDecimal(value)) {
        callback(new Error('请输入0.01~99999.99范围金额（小数位不超过2位）'))
      } else {
        callback()
      }
    }
    return {
      /**
         * 校验字段相关
         */
      rules: {
        price: [
          {
            validator: validatorPrice,
            trigger: ['blur', 'change'],
            required: true
          }
        ]
      },
      // 配置
      resolve: null,
      reject: null,
      // 状态组
      loading: false,
      dialogVisible: null,
      // 数据
      productInfo: {
        name: '',
        basePrice: ''
      },
      form: {
        days: [],
        shopId: '',
        productIds: [],
        price: ''
      }
    }
  },
  methods: {
    open({ days, productInfo }) {
      this.form.days = days.sort()
      this.form.shopId = productInfo.shopId
      this.form.productIds = [productInfo.id]
      this.productInfo = productInfo
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 按钮组
       */
    // 保存
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const params = Object.assign({}, this.form)
          params.price = this.$math.mul(params.price, 100, 0)
          this.loading = true
          modifyBatchProductParams(params)
            .then(res => {
              this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.resolve && this.resolve()
              this.hide()
            })
            .catch(err => {})
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
